package com.baicaoyuan.web.common;

import org.apache.log4j.Logger;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.Map;

/**
 * 功能描述：
 * <p>
 *
 * @author 罗富祥
 *         创建时间： 2017/6/24
 */
public class Log4jExtend {
    final static String LOG_BEGIN = " \n --------------------------------- LOG BEGIN ---------------------------------\n";
    final static String LOG_END = " \n --------------------------------- LOG END  ---------------------------------\n";
    private Logger logger = Logger.getLogger(getClass());

    public void Info(String msg) {
        logger.info(LOG_BEGIN + msg + LOG_END);
    }

    public void Error(String title, Exception ex) {

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        try {
            ex.printStackTrace(pw);
            logger.error(String.format(" %s title {%s} ， content:{%n %s %n}  %s", LOG_BEGIN, title, sw.toString(), LOG_END));
        } finally {
            pw.close();
        }


    }


    /**
     * 打印请求参数
     *
     * @param reqParam 请求参数列表
     */
    public void printRequestLog(Map<String, String> reqParam) {
        Info(LOG_BEGIN);
        Iterator<Map.Entry<String, String>> it = reqParam.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> en = it.next();
            Info("[" + en.getKey() + "] = [" + en.getValue() + "]");
        }
        Info(LOG_END);
    }


    /**
     * 打印返回值
     *
     * @param res 返回内容
     */
    public void printResponseLog(String res) {
        Info(LOG_BEGIN);
        Info(res);
        Info(LOG_END);
    }
}
